/*
 *  SaskuServer.cpp
 *  SaskuServer
 *
 *  Created by Einar Pius on 03/07/2008.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "SaskuServer.h"
#include "ConnectionHandler.h"
#include <Poco/Thread.h>
#include <Poco/Net/ServerSocket.h>
#include <Poco/Net/SocketAcceptor.h>
#include <Poco/Net/SocketReactor.h>

SaskuServer::SaskuServer() {}

SaskuServer::~SaskuServer() {}

void SaskuServer::initialize(Application& self)
{
	loadConfiguration();
	ServerApplication::initialize(self);
	logger().information("Server starting.");
}

int  SaskuServer::main(const std::vector<std::string>&)
{
	const unsigned short port = config().getInt("server.port");
	Poco::Net::ServerSocket svs(port);
	Poco::Net::SocketReactor reactor;
	Poco::Net::SocketAcceptor<ConnectionHandler> acceptor(svs, reactor);
	
	Poco::Thread thread;
	thread.start(reactor);
	logger().information("Server running.");
	
	waitForTerminationRequest();
	reactor.stop();
	thread.join();
	
	return Application::EXIT_OK;
}

void  SaskuServer::uninitialize()
{
	logger().information("Server shutting down.");
	ServerApplication::uninitialize();
}

POCO_SERVER_MAIN(SaskuServer);
